<?php
declare(strict_types=1);
namespace app\common\server;

class Sha256
{

    /**
     * 获取指定长度的随机字母数字组合的字符串
     *
     * @param int $length
     * @param int|null $type
     * @param string $addChars
     * @return string
     */
    public static function random(int $length = 6, int $type = null, string $addChars = ''): string
    {
        $str = '';
//        switch ($type) {
//            case 0:
//                $chars = str_repeat('0123456789', 3);
//                break;
//            case 1:
//                $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
//                break;
//            default:
//                $chars = 'abcdefghijkmnpqrstuvwxyz23456789' . $addChars;
//                break;
//        }
        $chars = 'abcdefghijkmnpqrstuvwxyz23456789' . $addChars;

        if ($length > 10) {
            $chars = $type == 1 ? str_repeat($chars, $length) : str_repeat($chars, 5);
        }
        if ($type != 4) {
            $chars = str_shuffle($chars);
            $str = substr($chars, 0, $length);
        } else {
            for ($i = 0; $i < $length; $i++) {
                $str .= mb_substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
            }
        }
        return $str;
    }
}